<?
class userModel extends db_abstract implements db_interface{

	private static $instance;
	
	public function __construct($iDb){
	
		parent::__construct($iDb);
	
	}
	
	
	public static function getInstance($iDb){
	
		if(self::$instance == NULL){
			self::$instance = new self($iDb);
		}
		
		return self::$instance;
	
	
	}
	
	
	public function validateLogin(user $iUser){
	
	// kijken of de combinatie in de db zit	
	$validate =	$this -> findInDb(array(
								"geb_naam"	=>		$iUser ->  name,
								"geb_pas"	=>		$iUser -> password,
								"geb_verwijderd" => 0
							), "gebruikers");
	// bool returnen							
	return $validate;
	
	}
	
	
	public function fillFromLogin(user $iUser){
	
		$query = $this -> db -> prepare("SELECT geb_id, geb_email, geb_type, geb_tel_nr  FROM gebruikers WHERE geb_naam = ? AND geb_pas = ?");
		
		$query = $this -> bindVars($query, array($iUser -> name, $iUser -> password));
		$query -> execute();
		
		$query -> bind_result($id, $email, $type, $tel); 
		
		$query -> fetch();
		
		
		
		$iUser -> id = $id;
	
		$iUser -> email = $email;
		$iUser -> type = $type;
		$iUser -> tel = $tel;
		
		return $iUser;
	
	}
	
	
	
	public function fill($iUser){
	
		$query = $this -> db -> prepare("SELECT geb_id, geb_naam ,geb_email, geb_type, geb_tel_nr FROM gebruikers WHERE geb_id = ?");
		
		$bindValue = $iUser -> id;
		
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id, $name ,$email, $type, $tel); 
		
		$query -> fetch();
		
		
		
		$iUser -> id = $id;
		$iUser -> name = $name;
		$iUser -> email = $email;
		$iUser -> type = $type;
		$iUser -> tel = $tel;
		
		return $iUser;
	
	}

	
	public function insertSessionKey(user $iUser){
	

		$query = $this -> db -> query("UPDATE gebruikers SET geb_session_key = '".$iUser -> getSessionKey()."' WHERE geb_id = ". $iUser -> id ."");
		
		if(mysqli_affected_rows($this -> db) > 0){
			return true;
		}else{
		
			return false;
		
		}
		
	}
	
	public function getIdFromSessionKey(user $iUser){
	
		$query = $this -> db -> prepare("SELECT geb_id, geb_type FROM gebruikers WHERE geb_session_key = ?");
		
		$bindVar =  $iUser -> getSessionKey();
		
		
		
		$query -> bind_param('s',$bindVar);
		
		$query -> execute();
		
		$query -> bind_result($id,$type);
		$query -> fetch();
			
		$iUser -> id = $id;
		$iUser -> type = $type;
	
		
		
		
		
	
	}
	
	public function deleteSessionKey(user $iUser){
	
		$this -> db -> query("UPDATE gebruikers SET geb_session_key = NULL WHERE geb_id = ".$iUser -> id."");
		
	}
	
	
	public function delete($id){
	
		$query = $this -> db -> prepare("UPDATE gebruikers SET geb_verwijderd = 1 WHERE geb_id = ?");
		
		$bindVar =  $id;
		
		
		
		$query -> bind_param('i',$bindVar);
		
		$query -> execute();
	
	}

	
	
	
}



?>